﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SelectGroupPanel : MonoBehaviour
{
    public Transform viewRoot;
    public GroupItem itemPrefab;

    public InputField actionIdInput, scaleInput, DescInput;

    List<GroupItem> cacheLists = new List<GroupItem>();
    public void OpenSelectGroupPanel()
    {
        gameObject.SetActive(true);
        viewRoot.DestroyChildren();
        cacheLists.Clear();
        var groupList = EActionDataCtr.Instance.GetGroupList();
        for (int i = 0; i < groupList.Count; i++)
        {
            GroupItem item = Createitem(groupList[i]);
            cacheLists.Add(item);
        }

        MainData();
    }
    private void MainData()
    {
        var mainData = EActionDataCtr.Instance.mainData;
        actionIdInput.text = mainData.ID.ToString();
        scaleInput.text = mainData.Scale.X + "," + mainData.Scale.Y + "," + mainData.Scale.Z;
        DescInput.text = mainData.Desc;
    }

    private GroupItem Createitem(Data.ActionGroup group)
    {
        var item = Instantiate<GroupItem>(itemPrefab, viewRoot);
        item.SetData(group);
        item.gameObject.SetActive(true);
        return item;
    }

    /// <summary>
    /// 新建一个Group数据
    /// </summary>
    public void CreateGroup()
    {
        var group = EActionDataCtr.Instance.AddGroupData();
        var item = Createitem(group);  
    }

    /// <summary>
    /// 删除一个group 数据的预设
    /// </summary>
    /// <param name="data"></param>
    public void DelateGroup(Data.ActionGroup data)
    {
        if (data != null)
        {
            for (int i = cacheLists.Count; i >=0 ; i--)
            {
                if (cacheLists[i].GetData() == data)
                {
                    Destroy(cacheLists[i].gameObject);
                    cacheLists.RemoveAt(i);
                    EActionDataCtr.Instance.DelateGroup(data);
                }
            }
        }
    }

    public void Close()
    {
        var mainData = EActionDataCtr.Instance.mainData;
        mainData.ID =   int.Parse(actionIdInput.text.Trim());
        string[] arr = scaleInput.text.Trim().Split(',');
        mainData.Scale = new Data.Vector3(float.Parse(arr[0]), float.Parse(arr[1]), float.Parse(arr[2]));
        mainData.Desc = DescInput.text;
        gameObject.SetActive(false);
    }
    /// <summary>
    /// 选中一个Group进行更新
    /// </summary>
    public void SelectActionGroup()
    {
        Data.ActionGroup selectGroup = null;
        for (int i = 0; i < cacheLists.Count; i++)
        {
            if (cacheLists[i].GetSelect() == true)
            {
                selectGroup = cacheLists[i].GetData();
            }
        }
        //Close();


        EActionDataCtr.Instance.SetCurGroupData(selectGroup);
        //选择一个Group
        EventManager.TriggerEvent<Data.ActionGroup>(CommonEvent.SelectActionGroup, selectGroup);
    }
    public void BuildActionCache()
    {
        var mainData = EActionDataCtr.Instance.mainData;
        foreach (var item in mainData.ActionGroups)
        {
            item.BuildActionCache();
        }
    }
}
